package com.syq.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.syq.constant.RabbitMQConstants;
import com.syq.model.StandardSubmit;
import com.syq.service.FilterChainManager;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName: RabbitMqListener
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/25 11:06
 */

@Component
public class RabbitMqListener {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private FilterChainManager filterChainManager;

    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(name = RabbitMQConstants.TOPIC_PRE_SEND+"_exchange",type = "topic"),
            value = @Queue(name = RabbitMQConstants.TOPIC_PRE_SEND,durable = "true",autoDelete = "false"),
            key = RabbitMQConstants.BINDING
    )})
    public void receiverMessageSendSms(StandardSubmit standardSubmit, Channel channel, Message message){

        try {

            System.out.println("策略模块接受消息："+objectMapper.writeValueAsString(standardSubmit));
            filterChainManager.executeFilter(standardSubmit);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
